package com.tokenplus.thai.home.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.tokenplus.thai.R
import com.tokenplus.thai.adata.user.RevDataGetUserAccount
import com.tokenplus.thai.extend.isLink
import com.tokenplus.thai.extend.toFinanceString

class HomeAssetsAdapter(private val dataList: List<RevDataGetUserAccount>) :
    RecyclerView.Adapter<HomeAssetsAdapter.ViewHolder>() {

    //点击事件
    private var mItemClickListener: (RevDataGetUserAccount) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAdapterHomeAssetsContent: RelativeLayout =
            view.findViewById(R.id.rlAdapterHomeAssetsContent)
        var imageAdapterHomeAssets: ImageView =
            view.findViewById(R.id.imageAdapterHomeAssets)
        var textAdapterHomeAssetsName: TextView =
            view.findViewById(R.id.textAdapterHomeAssetsName)
        var textAdapterHomeAssetsNameRef: TextView =
            view.findViewById(R.id.textAdapterHomeAssetsNameRef)
        var textAdapterHomeAssetsAmount: TextView =
            view.findViewById(R.id.textAdapterHomeAssetsAmount)
        var textAdapterHomeAssetsAmountRef: TextView =
            view.findViewById(R.id.textAdapterHomeAssetsAmountRef)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_home_assets, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataGetUserAccount = dataList[position]
        holder.textAdapterHomeAssetsName.text = data.coinName
        holder.textAdapterHomeAssetsAmount.text = data.balance?.toFinanceString()

        holder.rlAdapterHomeAssetsContent.setOnClickListener {
            mItemClickListener.invoke(data)
        }

        if (data.logo?.isLink() == true) {
            Glide.with(holder.imageAdapterHomeAssets.context).load(data.logo)
                .into(holder.imageAdapterHomeAssets)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (RevDataGetUserAccount) -> Unit) {
        mItemClickListener = listener
    }
}